<?php
/* Copyright (c) 2008 Hailey Bannister
All rights reserved.
 
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 
    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the aframephp project nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 */
 
/*
 * afMarkupParser.php - Hailey Bannister
 
 * This file holds the class definitions for afMarkupParser, which will
 * parse our own little 'ghetto' version of our markup.
 * Right now it lacks a ton of features and such, but ima work on it.
 * I ripped a lot of the regex from PodWiki, located at http://www.daemon.de/PodWiki
 * their parsing seemed to be simple enough.
 */

class afMarkupParser {
   private $headers;
 
        //This is our constructor class.. you should use it if needed.
   function __construct() {
      debug ("Yay class afMarkupParser loaded!",0);
   }
    
   function format ($text) {
	 	$nodes = preg_split('/\n{2,}/',$text);
		foreach ($nodes as $value) {
		if(preg_match('/^\s*$/',$value)){ continue; }
			$output = $this->do_code($value);
			$data .= "<p>$output</p>\n";
		}
		//Determining the table of contents needs to be done after
		//everything is processed...
		$data = preg_replace_callback('/(?<=\n|)\{toc\}/',array($this,'print_toc'),$data);
		return $data;
	}
	
	function do_code($text) {
		$scratch = $text;
		//url blocks
		//This will make a callback to a url using the 'validate_url($url,$desc)' method.
		$scratch = preg_replace_callback('/\(((http|https|ftp)(:\/\/[^\s]*))\)/',array($this,'validate_url'),$scratch);
		//internal urls
		$scratch = preg_replace_callback('/\[([^\]]*)\]/',array($this,'validate_internal_url'),$scratch);
	
		//Lets convert the shorthand..
		//*bold*
		// /italic/
		$scratch = preg_replace('/(?<![<\/])\*([^\*]+)\*/','<b>$1</b>',$scratch);
		$scratch = preg_replace('/(?<![<\/])\/([^\/]+)(?<!<)\//','<i>$1</i>',$scratch);

		//~~color:text~~ function
		$scratch = preg_replace('/\~\~([^\:]+):([^\~]+)\~\~/','<span style="color:$1;">$2</span>',$scratch);
		//===underline===
		$scratch = preg_replace('/===([^\=]+)===/','<span style="text-decoration:underline;">$1</span>',$scratch);
		//::centered text::
		$scratch = preg_replace('/::([^\:]+)::/','<div align="center">$1<\/div>',$scratch);
		//-+monospace+-
		$scratch = preg_replace('/-\+(.*?)\+-/','<tt>$1</tt>',$scratch);

		//bracketed text	
		$scratch = preg_replace('/\[\[([^\]]*)\]/','[$1]',$scratch);
	
		//Headers!
		$scratch = preg_replace_callback('/(?<=\n|)([^\n]+)\n(\^|\*|=|~){3,}/',array($this,'do_headers'),$scratch);

		// --- horizontal rules!
		$scratch = preg_replace('/(^|\n)---.*?($|\n)/','$1<hr />$2',$scratch);
		// Resolve entities..
		$scratch = preg_replace('/E<([^>]*)>/','&$1;',$scratch);
		return $scratch;
	}
	function validate_url($url) {
		$href = $url[1];
		$url_title = (count($url) > 2) ? $url[2] : $href;
		$url_title = (strcmp($url[0],$href)) ? $href : '';
	
		//Insert code here if we need to do more to the URLs...
		return "<a href='$href' class='$url_class'>$url_title</a>";
	}
	function validate_internal_url($url) {
		//Sometimes we get a value that looks like [link which is what happens
		//if you put too many [[ and ]]s places.
		//Insert code here if we need to do more to the URLs...
	
		$href = $url[1];
		$url_title = (count($url) > 2) ? $url[2] : $href;
		return "<a href='self.php/$href' class='$url_class'>$url[1]</a>";
	}
	function do_headers ($data) {
		$title = $data[1];
		$h = array('^' => 1, '*' => 2, '=' => 3, '~'=> 4);
		$size = $h[$data[2]];
		$this->headers[] = $title;
		return "\n<a href=$title><h$size>$title</h$size></a>\n";
	}
	function print_toc () {
			$contents .= "<b>Table of contents</b><br />\n";
		foreach ($this->headers as $value) {
			$contents .= "<a href='#$value>$value</a><br />\n";
		}
			return $contents;
	}
}
?>